import Taro, { Component } from '@tarojs/taro'
import { View, Text, Block, Image, Navigator, ScrollView } from '@tarojs/components'

import allExtrance from './../../../config/allExtrance'
import allRequest from './../../../api/allRequest'
import { Tabbar, Footer, MoreFormid } from './../../../component/index'
import './index.less'

let _this

class Index extends Component {

    config = {
        navigationBarTitleText: '首页',
        // enablePullDownRefresh: true,
        onReachBottomDistance: 1,
    }

    constructor() {
        super()
        Taro.setNavigationBarColor({
            frontColor: Taro.getStorageSync('secondColor'),
            backgroundColor: Taro.getStorageSync('baseColor')
        })
        this.state = {
            position: [],
            worker: [],
            filter: 1,
            jobId: 0,
            page: 0,
            getDataFinished: false,
            hasShop: false,
            educationArr: ['未知', '小学', '初中', '中专', '高中', '大学专科', '大学本科', '硕士', '博士', '博士后', '其它'],
            experienceArr: ['无经验', '1年', '2年', '3年', '4年', '5年', '6年', '7年', '8年', '9年', '10年'],
            genderArr: ['未知', '男', '女'],
        }
    }

    componentWillReceiveProps(nextProps) {
        console.log(this.props, nextProps)
    }

    componentDidMount() {
        _this = this
        // this.getData()
        //缓存用户处于企业端
        Taro.setStorageSync('situation', 'business')
    }

    componentWillUnmount() { }

    componentDidShow() {
        this.getData()
    }

    componentDidHide() { }

    // onPullDownRefresh() {
    //     this.setState({
    //         worker: [],
    //         page: 0,
    //         getDataFinished: false
    //     }, () => {
    //         this.getData()
    //     })

    //     Taro.stopPullDownRefresh()
    // }

    // onReachBottom() {
    //     this.getData()
    // }

    refresh() {
        _this.setState({
            worker: [],
            page: 0,
            getDataFinished: false
        }, () => {
            _this.getData()
        })

        Taro.stopPullDownRefresh()
    }

    getMore() {
        _this.getData()
    }

    classTap(jobId, e) {
        this.setState({
            jobId,
            getDataFinished: false,
            page: 0,
            worker: [],
        }, () => this.getData())
    }

    getData() {
        let {
            filter,
            jobId,
            page,
            getDataFinished
        } = this.state
        if (getDataFinished) {
            return
        }
        allExtrance.util.Taro.showLoading('加载中')
        this.setState({ canTap: false })

        allRequest.companyResume({ 'tab': filter, 'jobId': jobId, 'page': ++page })
            .then((res) => {
                let worker = res.data

                if (allExtrance.util.isArray(worker)) {
                    this.setState(preState => ({ worker: preState.worker.concat(worker), page, getDataFinished: worker.length < 10 ? true : false }))
                    setTimeout(() => { 
                        this.setState({ canTap: true }, () => {
                            Taro.hideLoading()
                        })
                    }, 500)
                }
            })
            .catch((err) => {
                allExtrance.util.Taro.showToast(err)
            })
    }

    navTap(filter, e) {
        if (this.state.canTap) {
            this.setState({
                filter,
                getDataFinished: false,
                page: 0,
                worker: [],
            }, () => this.getData())
        }
    }

    toResumeInfo(id, index, e) {
        Taro.navigateTo({
            url: '/subPackages/subPackageF/resumeInfo/resumeInfo?workerApplyId=' + id
        })
        setTimeout(() => {
            let { worker } = this.state
            worker[index].apply_status = 2;
            this.setState({ worker })
        }, 1500)
    }

    jsGetAge(strBirthday) {
        let returnAge;
        //以下五行是为了获取出生年月日，如果是从身份证上获取需要稍微改变一下
        let strBirthdayArr = strBirthday.split("-");
        let birthYear = strBirthdayArr[0];
        let birthMonth = strBirthdayArr[1];
        let birthDay = strBirthdayArr[2];

        let d = new Date();
        let nowYear = d.getFullYear();
        let nowMonth = d.getMonth() + 1;
        let nowDay = d.getDate();
        if (nowYear == birthYear) {
            returnAge = 0;//同年 则为0岁
        } else {
            let ageDiff = nowYear - birthYear; //年之差
            if (ageDiff > 0) {
                if (nowMonth == birthMonth) {
                    let dayDiff = nowDay - birthDay;//日之差
                    if (dayDiff < 0) {
                        returnAge = ageDiff - 1;
                    } else {
                        returnAge = ageDiff;
                    }
                }
                else {
                    let monthDiff = nowMonth - birthMonth;//月之差
                    if (monthDiff < 0) {
                        returnAge = ageDiff - 1;
                    } else {
                        returnAge = ageDiff;
                    }
                }
            }
            else {
                returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
            }
        }
        return returnAge;//返回周岁年龄
    }

    render() {
        let { worker, educationArr, experienceArr, genderArr, getDataFinished, filter, hasShop } = this.state
        const currentPage = 'businessResume'
        return (
            <View className='businessIndex'>
                <View className={this.state.position.length > 1 ? 'page padding-top-1' : 'page'}>
                    <View className='header'>
                        {
                            this.state.position.length > 1 ? <View className='class' style={`background:${Taro.getStorageSync('baseColor')}`}>
                                <View className='list'>
                                    {
                                        this.state.position.map((item, unique) => <View
                                            className={this.state.jobId == item.jobId ? 'item current' : 'item'}
                                            onClick={this.classTap.bind(this, item.jobId)}>
                                            {item.name}
                                        </View>
                                        )
                                    }
                                </View>
                            </View> : null
                        }
                        <MoreFormid>
                            <View className='nav'>
                                <View className={`item ${this.state.filter == 1 ? 'current' : ''}`} onClick={this.navTap.bind(this, 1)}>全部简历{this.state.filter == 1 && <View className='xiabiao' style={`background-color:${Taro.getStorageSync('baseColor')}`}></View>}</View>
                                <View className={`item ${this.state.filter == 2 ? 'current' : ''}`} onClick={this.navTap.bind(this, 2)}>已沟通{this.state.filter == 2 && <View className='xiabiao' style={`background-color:${Taro.getStorageSync('baseColor')}`}></View>}</View>
                                <View className={`item ${this.state.filter == 3 ? 'current' : ''}`} onClick={this.navTap.bind(this, 3)}>不合适{this.state.filter == 3 && <View className='xiabiao' style={`background-color:${Taro.getStorageSync('baseColor')}`}></View>}</View>
                            </View>
                        </MoreFormid>
                    </View >
                    <ScrollView style='height:92vh' scrollY onScrollToLower={this.getMore}>
                        <MoreFormid>
                            {
                                (worker && worker.length > 0) ? <Block>
                                    <View className='resume-list'>
                                        {
                                            worker.map((item, unique) =>
                                                <View className='resume-cell' onClick={this.toResumeInfo.bind(this, item.worker_apply_id, unique)} hover-className='none'>
                                                    <View className='title-row'>
                                                        <View className='title'>
                                                            <Text className='text'>{item.job.position_title}</Text>
                                                        </View>
                                                        {item.apply_status == 1 && <View className='read-status'>未读</View>}
                                                        {item.apply_status == 2 && <View className='read-status read'>已读</View>}


                                                    </View>
                                                    <View className='info'>
                                                        <Image className='avatar' mode='aspectFill' src={item.worker.worker_avatar} lazy-load='{{true}}' />
                                                        <View className='summary'>
                                                            <View className='row-1'>
                                                                <View className='name'>{item.worker.worker_nickname}</View>
                                                            </View>
                                                            <View className='row-2'>
                                                                <View className='item'>{item.worker && genderArr[Number(item.worker.worker_gender)]}</View>
                                                                <View className='item'>{item.worker && educationArr[Number(item.worker.worker_education)]}</View>
                                                                <View className='item'>{item.worker && this.jsGetAge(item.worker.worker_birthday)}岁</View>
                                                                {item.worker.worker_experience > 0 ? <View className='item'>{item.worker.worker_experience}年</View> : <View className='item'>无经验</View>}

                                                            </View>
                                                            <View className='row-3'>{item.time_create}{' '}投递</View>
                                                        </View>
                                                    </View>
                                                </View>
                                            )
                                        }
                                    </View >
                                    <Footer></Footer>
                                    {/* {
                                        getDataFinished &&
                                        // <View className='finished-tip'>没有数据啦~</View>
                                        <Footer></Footer>
                                    } */}
                                </Block > : <Block>
                                        <View className='empty'>
                                            <Image className='image' src='https://file.open.banchengyun.com/2019/03/28/155374420936342.png' />
                                            <View className='text'>
                                                {filter === 1 && <View>目前还没有人申请职位哦</View>}
                                                {filter === 2 && <View>目前还没有已沟通职位哦</View>}
                                                {filter === 3 && <View>目前还没有不合适职位哦</View>}

                                                {
                                                    hasShop ? <Block>
                                                        <View className='guide'>建议去<Navigator url='/subPackages/subPackageF/jobManage/jobManage' hover-className='none'>我的 - 职位管理</Navigator>中优化职位信息</View>
                                                    </Block> : <Block>
                                                            <View className='guide'>建议去<Navigator url='/subPackages/subPackageF/myShop/myShop' hover-className='none'>我的 - 我的商铺</Navigator>中完善商铺信息</View>
                                                        </Block>
                                                }
                                            </View >
                                        </View >
                                    </Block >
                            }
                        </MoreFormid>
                    </ScrollView>
                </View >
                <Tabbar currentPage={currentPage}></Tabbar>
            </View >
        )
    }
}

export default Index
